package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.boats.business.impl.BoatsSystemServiceImpl;
import com.ship.boats.domain.SpBoatsReceivingStation;
import com.ship.boats.domain.SpBoatsWeather;
import com.ship.boats.domain.vo.QueryBoatsWeatherInfo;
import com.ship.boats.mapper.BoatsWeatherMapper;
import com.ship.boats.service.BoatsReceivingStationService;
import com.ship.boats.service.BoatsWeatherService;
import com.ship.common.core.constant.ShipApiConstants;
import com.ship.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <b>BoatsWeatherServiceImpl</b>
 *
 * @description: BoatsWeatherServiceImpl <br>
 * @date: 2024/4/16 16:55 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class BoatsWeatherServiceImpl extends ServiceImpl<BoatsWeatherMapper, SpBoatsWeather> implements BoatsWeatherService {
    private static final Logger logger = LoggerFactory.getLogger(BoatsWeatherServiceImpl.class);

    @Autowired
    private BoatsReceivingStationService boatsReceivingStationService;

    @Autowired
    private BoatsSystemServiceImpl boatsSystemServiceImpl;

    @Override
    public void getBoatsWeather() {
        List<SpBoatsReceivingStation> receivingStationList = boatsReceivingStationService.queryReceivingStationList();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY,-4);
        Date time = calendar.getTime();
        String date = DateUtil.format(time, DatePattern.PURE_DATETIME_FORMATTER);
        if (CollectionUtil.isNotEmpty(receivingStationList)){
            receivingStationList.forEach(item -> {
                Map<String,String> map = new HashMap<>();
                map.put("dateTime",date);
                map.put("lon",String.valueOf(item.getLon()));
                map.put("lat",String.valueOf(item.getLat()));
                Object data = boatsSystemServiceImpl.getBoatsUtil(map, ShipApiConstants.RECEIVING_STATION_URL);
                if (null != data){
                    SpBoatsWeather boatsWeather = JSONUtil.toBean(JSONUtil.parseObj(data), SpBoatsWeather.class);
//                    List<SpBoatsWeather> boatsWeatherList = JSONUtil.toList(JSONUtil.parseArray(data), SpBoatsWeather.class);
                    boatsWeather.setLat(item.getLat());
                    boatsWeather.setLon(item.getLon());
                    boatsWeather.setEnable("1");
                    boatsWeather.setCreateTime(new Date());
                    boatsWeather.setUpdateTime(new Date());
//                    if (CollectionUtil.isNotEmpty(boatsWeatherList)){
//                        boatsWeatherList.forEach(value -> {
//                            value.setLat(item.getLat());
//                            value.setLon(item.getLon());
//                            value.setEnable("1");
//                            value.setCreateTime(new Date());
//                            value.setUpdateTime(new Date());
//                        });
//                        this.saveBatch(boatsWeatherList);
                    //}
                    baseMapper.insert(boatsWeather);
                }
            });
        }
    }

    @Override
    public PageInfo<QueryBoatsWeatherInfo> queryBoatsWeatherList(Integer pageNum, Integer pageSize, String id) {
        PageHelper.startPage(pageNum,pageSize);
        List<QueryBoatsWeatherInfo> boatsWeatherList = baseMapper.queryBoatsWeatherList(id);
        PageInfo<QueryBoatsWeatherInfo> pageInfo = new PageInfo<>(boatsWeatherList);
        return pageInfo;
    }

    @Override
    public List<QueryBoatsWeatherInfo> exportBoatsWeather(String id) {
        return baseMapper.queryBoatsWeatherList(id);
    }
}
